package com.mxl;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author mxl
 * @description: Map测试
 * @date 2022-02-17 13:55
 */
@Slf4j
public class MapTest {


    public static void main(String[] args) {

        //定义 map
        Map<String, Integer> map = new HashMap<String, Integer>();

        //map添加数据
        map.put("1", 1);
        map.put("2", 2);
        map.put("3", 3);

        export(map);
        //按照key删除
        map.remove("1");
        //按照key和value删除
        map.remove("3", 3);
        export(map);
    }

    /**
     * 输出map中的数据
     *
     * @param map
     */
    public static void export(Map<String, Integer> map) {
        //foreach 循环遍历
        for (String str : map.keySet()) {
            log.info("key:" + str + " value:" + map.get(str));
        }

        /**
         //通过keySet遍历
         for (String key : map.keySet()) {
         System.out.println(key);
         }

         //通过values遍历
         for (Integer value : map.values()) {
         System.out.println(value);
         }

         //迭代器遍历
         Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
         while (it.hasNext()) {
         Map.Entry<String, Integer> entry = it.next();
         System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
         }
         //Lambda遍历
         map.forEach((key, value) -> {
         System.out.println("key:" +key + " value:" + value);
         });
         */
        log.info("===========");
    }

}
